package com.douglas.managesystem.controller.student;

import com.douglas.managesystem.common.ResponseResult;
import com.douglas.managesystem.common.SearchEntity;
import com.douglas.managesystem.controller.BaseController;
import com.douglas.managesystem.entity.studentpojo.Classpojo;
import com.douglas.managesystem.service.student.ClassService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Description TODO
 * @Author 以梦为馬 <Douglas1314@126.com>
 * @Version V1.0.0
 * @Date 2020-11-16 22:19
 **/
@Controller
@RequestMapping("/stu")
public class ClassController extends BaseController {


    @Autowired
    private ClassService classService;

    /**
     * 1.学生个人所有成绩 分页查询
     * @param model
     * @param params
     * @return
     */
    @GetMapping("/classPo/list")
    public String list(Model model, SearchEntity params){
        PageInfo<Classpojo> page = classService.findAllClassByPage(params);
        model.addAttribute("classPojo",page);
        return "admin/classPo/list";
    }

    /**
     * 2.新增或编辑班级
     * @param classpojo
     * @return
     */
    @PostMapping("/classPo/add")
    public String addOrEdit(Classpojo classpojo){
        System.out.println(classpojo);
        if(StringUtils.isEmpty(classpojo.getId())){
            //1.新增
            classService.saveClassPo(classpojo);
        }else{
            //2.编辑
            classService.updateClassPo(classpojo);
        }
        return "redirect:/stu/classPo/list";
    }

    /**
     * 3.根据id查询class对象
     * @param id
     * @return
     */
    @GetMapping("/classPo/getClassPo")
    @ResponseBody
    public ResponseResult getLessonById(@RequestParam("id") String id){
        ResponseResult responseResult = new ResponseResult();
        try {
            Classpojo classpojo = classService.findClassPoById(id);
            responseResult.setSuccess(true);
            responseResult.setData(classpojo);
            responseResult.setInfo("查询成功"+id);
            return responseResult;
        } catch (Exception e) {
            responseResult.setSuccess(false);
            responseResult.setInfo("查询失败"+id);
        }
        return responseResult;
    }

    /**
     * 4.根据id删除科目信息 逻辑删除
     * @param id
     * @return
     */
    @GetMapping("/classPo/delete")
    public String delete(String id){
        classService.delete(id);
        return "redirect:/stu/classPo/list";
    }
}
